package com.breakfast.fun.base;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.breakfast.fun.App;
import com.breakfast.fun.util.AnalyticsUtil;
import com.breakfast.fun.util.CartAnimUtil;
import com.breakfast.fun.util.DialogUtil;
import com.breakfast.fun.util.ImageUtil;
import com.breakfast.fun.util.TUtil;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

import butterknife.ButterKnife;

/**
 * Created by hp on 2016/8/16.
 */
public abstract class BaseFragment<P extends BasePresenter, M extends BaseModel> extends Fragment {
    public P presenter;
    public M model;

    public Context context;

    private View view;

    protected DialogUtil mDialogUtil;
    protected Dialog mDialog;

    protected ImageUtil imageUtil;

    protected Tracker mTracker;

    protected CartAnimUtil mCartAnimUtil;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(this.getLayoutId(), container, false);
        ButterKnife.bind(this, view);
        context = getContext();
        imageUtil = new ImageUtil(getActivity());
        mDialogUtil = new DialogUtil(BaseFragment.this.getActivity());
        mCartAnimUtil = new CartAnimUtil(context);
        presenter = TUtil.getT(this, 0);
        model = TUtil.getT(this, 1);
        mTracker = App.getApp().getDefaultTracker();
        mTracker.setScreenName(AnalyticsUtil.translateClassName(this.getClass().getSimpleName()));
        this.initView();
        if (this instanceof BaseView) presenter.setVM(context, this, model);
        return view;
    }

    protected void showLoadingDialog(boolean isCanClose) {
        if (mDialog != null) {
            if (mDialog.isShowing()) {
                mDialog.dismiss();
            }
        }
        mDialog = mDialogUtil.getWaitDialog("加载中...", isCanClose);
    }

    protected void hideLoadingDialog() {
        if (mDialog != null) {
            if (mDialog.isShowing()) {
                mDialog.dismiss();
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (presenter != null) {
            presenter.onDestroy();
        }
        ButterKnife.unbind(this);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getActivity().getWindow().setAttributes(lp);
    }

    @Override
    public void onResume() {
        super.onResume();
        mTracker.send(new HitBuilders.ScreenViewBuilder().build());
    }

    public abstract int getLayoutId();

    public abstract void initView();
}
